<template>
  <div class="renwu">
    <el-button type="primary" @click="editRow()">新增</el-button>
    <el-table v-loading="loading" :data="tableData" style="width: 100%">
      <el-table-column fixed prop="name" label="任务名称" />
      <el-table-column prop="url" label="地址">
        <template slot-scope="scope">
          <el-popover v-if="scope.row.url" placement="top-start" title="" width="500" trigger="hover" :content="scope.row.url">
            <el-button slot="reference" @click="copyToClip(JSON.stringify(scope.row.url))">复制</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="method" label="请求方式" />
      <el-table-column prop="data" label="数据">
        <template slot-scope="scope">
          <el-button type="primary" plain @click="copyToClip(JSON.stringify(scope.row.data))">复制</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="headers" label="headers">
        <template slot-scope="scope">
          <el-button type="success" plain @click="copyToClip(JSON.stringify(scope.row.headers))">复制</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="Cookies" label="Cookies">
        <template slot-scope="scope">
          <el-popover v-if="scope.row.Cookies" placement="top-start" title="" width="300" trigger="hover" :content="scope.row.Cookies">
            <el-button slot="reference">查看</el-button>
          </el-popover>
          <template v-else>{{ '无Cookies' }}</template>
        </template>
      </el-table-column>
      <el-table-column prop="data" label="是否每天执行">
        <template slot-scope="scope">
          {{ scope.row.isEveryday ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间/更新/任务时间" width="220">
        <template slot-scope="scope">
          <i class="el-icon-time" /><span>{{ scope.row.createTime| timeFilter }}</span><br>
          <i class="el-icon-time" /><span>{{ scope.row.updateTime| timeFilter }}</span><br>
          <i class="el-icon-timer" /><span>{{ scope.row.cron| timeFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-edit" circle @click.native.prevent="editRow(scope.row)" />
          <el-button
            type="success"
            size="small"
            icon="el-icon-document-copy"
            circle
            @click.native.prevent="editRow(scope.row,true)"
          />
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            circle
            @click.native.prevent="deleteRow(scope.row._id)"
          />
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="form.page"
      :page-size="form.pageSize"
      :total="total"
      :page-sizes="[5, 20, 50]"
      layout="total, sizes, prev, pager, next, jumper"
      @current-change="handlePageChange"
      @size-change="handleSizeChange"
    />
    <renwudialog v-if="dialog" :form="dialogForm" :copy="copy" @dialogVisible="dialogVisible" @dialogForm="submit" />
  </div>
</template>

<script>
import { destroy, renwuList, renwuSave } from '@/api/renwu'
import { parseTime } from '@/utils'
import renwudialog from '@/views/renwu/components/dialog'

export default {
  name: 'RenwuIndex',
  components: { renwudialog },
  filters: {
    timeFilter(time) {
      return parseTime(time)
    }
  },
  data() {
    return {
      loading: true,
      form: {
        page: 1,
        limit: 5
      },
      total: 0,
      tableData: [],
      dialogForm: {},
      dialog: false,
      copy: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.loading = true
      const { result, total } = await renwuList(this.form)
      this.tableData = result
      this.total = total
      this.loading = false
    },
    editRow(row, copy) {
      // Object.assign(this.$data, this.$options.data.call(this)) // 重置回最开始data里return定义的初始值
      this.dialog = true
      if (row) {
        row.data = JSON.stringify(row.data)
        row.headers = JSON.stringify(row.headers)
        this.dialogForm = row
        this.copy = copy || false
      } else {
        this.dialogForm = { isEveryday: true }
      }
    },
    dialogVisible(val) {
      this.dialog = val
      this.getList()
    },
    async submit(form) {
      const result = await renwuSave(form)
      if (result.code === 0) {
        this.$message.success(result.message)
        await this.getList()
      }
      console.log(result)
    },
    async deleteRow(id) {
      this.$confirm('此操作将永久删除该脚本, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const result = await destroy(id)
        this.$message.success(result.message)
        await this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    /**
     * 复制内容到粘贴板
     * content : 需要复制的内容
     */
    copyToClip(content) {
      const aux = document.createElement('input')
      aux.setAttribute('value', content)
      document.body.appendChild(aux)
      aux.select()
      document.execCommand('copy')
      document.body.removeChild(aux)
      if (content) {
        this.$message.success('复制成功')
      } else {
        this.$message.warning('无数据')
      }
    },
    handlePageChange(page) {
      this.form.page = page
      this.getList()
    },
    handleSizeChange(size) {
      this.form.page = 1
      this.form.limit = size
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.renwu {
  padding: 20px;

}
</style>
